package com.youlu.campus.service.commission;

import com.youlu.campus.entity.commission.DistributionContentConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Slf4j
@Service
public class DistributionContentConfigService {

    @Autowired
    private MongoTemplate mongoTemplate;


    public void saveOrUpdate(DistributionContentConfig distributionContentConfig){
        DistributionContentConfig distributionContentConfigExist = this.findOne();
        if(Objects.isNull(distributionContentConfigExist)){
            this.mongoTemplate.save(distributionContentConfig);
            return;
        }
        String id = distributionContentConfigExist.getId();
        BeanUtils.copyProperties(distributionContentConfig,distributionContentConfigExist);
        distributionContentConfigExist.setUpdatedTime(new Date());
        distributionContentConfigExist.setId(id);
        this.mongoTemplate.save(distributionContentConfigExist);
    }

    public DistributionContentConfig findOne(){
        return  mongoTemplate.findOne(new Query(), DistributionContentConfig.class);
    }

}
